package org.lzy.shop.viewmodel.repository.lmpl

import org.lzy.shop.network.KtorApiService
import org.lzy.shop.response.CouponResponse
import org.lzy.shop.response.HomeGoodAllResponse
import org.lzy.shop.response.HomeResponse
import org.lzy.shop.viewmodel.repository.BaseRepository
import org.lzy.shop.viewmodel.repository.HomeRepository

/**
 * 首页数据仓库实现类
 */
class HomeRepositoryImpl : BaseRepository(), HomeRepository {

    override suspend fun getHomeData(): HomeResponse? {
        return safeApiCall {
            val response = KtorApiService.home()
            handleResponse(response)
        }
    }

    override suspend fun getHomeGoodsAll(): HomeGoodAllResponse? {
        return safeApiCall {
            val response = KtorApiService.getHomeGoodsAll()
            handleResponse(response)
        }
    }

    override suspend fun getCoupon(): CouponResponse? {
        return safeApiCall {
            val response = KtorApiService.getCoupon()
            handleResponse(response)
        }
    }

    override suspend fun couponReceive(couponId: Int?): String? {
        if (couponId == null) {
            return null
        }

        return safeApiCall {
            val response = KtorApiService.couponReceive(couponId)
            handleResponse(response)
        }
    }
}